package znbkListXtfx.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;

import znbkListRdqh.isNeedPaperAssembly.ILoadDateViewAssembly;
import znbkListXtfx.entity.SystemReviewListEntity;
import znbkListXtfx.paperAssembly.SystemReviewMakePaperPresenter;
import znbkListXtfx.paperAssemblyRecordInfo.PaperAssemblyRecordPresenter;
import znbkListXtfx.presenter.SystemReviewListPresenter;
import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import znbkListXtfx.adapter.SystemReviewListAdapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018\1\29 0029.
 */

public class SystemReviewListFragment extends BaseMainFragment<SystemReviewListPresenter> implements View.OnClickListener, ILoadMoreDataView<SystemReviewListEntity> ,ILoadDateViewAssembly {


    public static int ThisPaperIndex = -1;
    public static int AnotherPaperIndex = -1;
    public static String ThisZsds = "";
    public static String ThisFilterItem = "";

    private SystemReviewListAdapter mSystemReviewListAdapter;
    private TextView mTvSpStatus;
    private TextView mTvTotal;
    private PopupMenuType1Adapter popupMenuSubjectAdapter;
    private PopupMenu subjectPopupMenu;
    private View masker;
    private RelativeLayout mSpLayout;
    public static TextView mTvToolbarTitle;
    private LinearLayout mLayoutToolbarBack;
    private Map<String,Integer> mQuesStatusMap = new HashMap<>();

    @Override
    protected void updateViews(boolean refresh) {
        mPresenter.getData(refresh);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_system_review;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {
        mPresenter = new SystemReviewListPresenter(this);

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        masker = view.findViewById(R.id.bg_masker);
        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
//        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_xtfx);
        mTvToolbarTitle = (TextView) view.findViewById(R.id.tv_toolbar_title);
        mTvToolbarTitle.setText(R.string.menu_xtfx);

//        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);
        mLayoutToolbarBack = (LinearLayout) view.findViewById(R.id.layout_toolbar_back);
        mLayoutToolbarBack.setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

        mTvSpStatus.setOnClickListener(this);
        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArraysXtfxRdqh);
        String[] quesStatusNameArrays = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusNameArrays[i]  = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }
        popupMenuSubjectAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusNameArrays);
        subjectPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        subjectPopupMenu.setListAdapter(popupMenuSubjectAdapter);
        subjectPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(subjectPopupMenu.getCheckedStr()));
            }
        });
        mTvSpStatus.setText(subjectPopupMenu.getCheckedStr());
        subjectPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpStatus.setSelected(false);
            }
        });


        mSystemReviewListAdapter = new SystemReviewListAdapter(getContext());
        mSystemReviewListAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mSystemReviewListAdapter);
        mSystemReviewListAdapter.setOnItemClickListener(new SystemReviewListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<SystemReviewListEntity.ModelPaperInfoListBean> list, int position) {
                //ToastUtils.showShort("第"+(position+1)+"个");
                mPresenter.openItemDetail(position,list.get(position));
            }
        });


    }


    @Override
    public void showNetError() {
        super.showNetError();
        setPaperTotal(0);

    }


    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        ThisPaperIndex = 1;
        setPaperTotal(0);

    }

    @Override
    public void hideLoading() {
        super.hideLoading();

    }

    @Override
    public void loadData(SystemReviewListEntity data) {
        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }

        ThisPaperIndex = data.getTotalCount()+2;
        setPaperTotal(data.getTotalCount());
        mSystemReviewListAdapter.clearAndAdd(data.getModelPaperInfoList());
        loadComplete(mSystemReviewListAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(SystemReviewListEntity data) {
        setPaperTotal(data.getTotalCount());
        mSystemReviewListAdapter.add(data.getModelPaperInfoList());
        loadComplete(mSystemReviewListAdapter.getAdapterItemCount() >= data.getTotalCount());
    }





    /**
     * 没有试卷或者加载失败的逻辑处理
     */
    private void setPaperTotal(int number) {
        String paperTotal = String.format(getString(R.string.znbkPaperTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }



    /*@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu,inflater);
        inflater.inflate(R.menu.menu_main,menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.item_other_system){
            //ToastUtils.showShort("基本复习进度提示");

            return true;
        }
        return false;
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_status:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpStatus.setSelected(true);
                subjectPopupMenu.showLocation(mSpLayout, 0, 0);
                break;
            case R.id.layout_toolbar_back:
                getHolderActivity().finish();
                break;
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        Method();
    }

    @Override
    public void onResume() {
        super.onResume();
//        Method();
    }

    @Override
    public void assemblyPaper() {
        SystemReviewMakePaper();
    }

    @Override
    public void assemblyPaperSuccessed() {
        updateViews(false);
    }


    /*
* 是否需要组卷
* */
    private PaperAssemblyRecordPresenter mPaperAssemblyRecordPresenter;
    private void IsNeedPaperAssembly(){
        mPaperAssemblyRecordPresenter = new PaperAssemblyRecordPresenter(this);
        mPaperAssemblyRecordPresenter.getData();
    }


    /*
    * 开始组卷
    * */
    private SystemReviewMakePaperPresenter mSystemReviewMakePaperPresenter;
    private void SystemReviewMakePaper(){
        mSystemReviewMakePaperPresenter = new SystemReviewMakePaperPresenter(this);
        mSystemReviewMakePaperPresenter.getData();
    }


//    private Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
////            //收到消息后显示当前时间
////            long current = System.currentTimeMillis();
////            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
////            String time = dateFormat.format(current);
////            mTv.setText(time);
//            IsNeedPaperAssembly();
//        }
//    };
//    private Timer timer = new Timer();
//    private TimerTask timerTask = new TimerTask() {
//        @Override
//        public void run() {
//            mHandler.sendEmptyMessage(0);
//        }
//    };
//    private boolean isStart = false;
//    public void Method() {
//        if (isStart) {
//            timer.cancel();
//            isStart = false;
//        } else {
//            timer.schedule(timerTask, 0, 5000);
//            isStart = true;
//        }
//    }



}